package com.topisv.tms.service;

import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.PipeStock;
import com.topisv.tms.repository.PipeStockDao;

//Spring Bean的标识.
@Component
//默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class PipeStockService {
	private PipeStockDao pipeStockDao;
	
	@Autowired
	public void setPipeStockDao(PipeStockDao pipeStockDao) {
		this.pipeStockDao = pipeStockDao;
	}
	
	public List<PipeStock> findByPipeCode(String pipe_code,String shelf_code)
	{
		return pipeStockDao.findByPipeCode(pipe_code, shelf_code);
	}
	
	public PipeStock findByStockCode(String stock_code)
	{
		return pipeStockDao.findByStockCode(stock_code);
	}
	
	public PipeStock getPipeStock(Long id) {
		return pipeStockDao.findOne(id);
	}
	
	@Transactional(readOnly = false)
	public void savePipeStock(PipeStock entity) {
		pipeStockDao.save(entity);
	}
	
	@Transactional(readOnly = false)
	public void deletePipeStock(Long id) {
		pipeStockDao.delete(id);
	}
	
	public List<PipeStock> getAllPipeStock() {
		return (List<PipeStock>) pipeStockDao.findAll();
	}

	/**
	 * 条件查询
	 * @param param
	 * @return
	 */
	public List<PipeStock> loadByParam(Map<String, Object> param) {
		Sort sort = new Sort(Sort.Direction.ASC,"id");
	    Specification<PipeStock> spec =this.getWhereClause(param);
	    List<PipeStock> list = this.pipeStockDao.findAll(spec, sort);
	    return list;
	}
	
	/**
	 * 条件封装
	 * @param params
	 * @return
	 */
	private Specification<PipeStock> getWhereClause(final Map<String, Object> params) {
		 return new Specification<PipeStock>(){
		    @Override
		    public Predicate toPredicate(Root<PipeStock> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
		      Predicate pre = cb.conjunction();
		      
		      //areagroup_code
		      String areagroup_code=(String) params.get("areagroup_code");
		      if(StringUtils.isNotBlank(areagroup_code)){
		    	  pre.getExpressions().add(cb.equal(r.get("areagroup_code"),areagroup_code));
		      }
			  
		      //customer_code
		      String customer_code=(String) params.get("customer_code");
		      if(StringUtils.isNotBlank(customer_code)){
		    	  pre.getExpressions().add(cb.equal(r.get("customer_code"),customer_code));
		      }
		      
		      //sku
		      String stock_code=(String) params.get("stock_code");
		      if(StringUtils.isNotBlank(stock_code)){
		    	  pre.getExpressions().add(cb.equal(r.get("stock_code"),stock_code));
			  }
		      
		      return pre;
		    }
		  };
	}

	public PipeStock loadByCondition(String customerCode,String warehouseCode, String stockCode) {
		return this.pipeStockDao.loadByCondition(customerCode,warehouseCode,stockCode);
	}
	
}
